<template>
  <div class="parent">
    <el-form :inline="true"
      :model="formInline"
      class="demo-form-inline"
      :rules="formInlineRules"
      ref="formInline"
      label-width="100px">
      <el-form-item prop="customerName">
        <el-input v-model="formInline.customerName" placeholder="客户名称"></el-input>
      </el-form-item>
      <el-form-item prop="customerAbbreviation">
        <el-input v-model="formInline.customerAbbreviation" placeholder="客户简称"></el-input>
      </el-form-item>
      <el-form-item prop="customerContact">
        <el-input v-model="formInline.customerContact" placeholder="客户联系人"></el-input>
      </el-form-item>
      <el-form-item prop="customerPhone">
        <el-input v-model="formInline.customerPhone" placeholder="客户电话"></el-input>
      </el-form-item>
      <el-form-item prop="customerAddress">
        <el-input v-model="formInline.customerAddress" placeholder="客户地址"></el-input>
      </el-form-item>
      <el-form-item>
        <el-select v-model="formInline.region" placeholder="代理商">
          <el-option label="是" value="shanghai"></el-option>
          <el-option label="否" value="beijing"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit('query')">查询</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit('add')">添加</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="tableData3"
      style="width: 100%;border: 1px solid #f6f6f6"
      height="450">
      <el-table-column
        fixed
        prop="date"
        label="日期"
        width="150">
      </el-table-column>
      <el-table-column
        prop="name"
        label="姓名"
        width="120">
      </el-table-column>
      <el-table-column
        prop="province"
        label="省份"
        width="120">
      </el-table-column>
      <el-table-column
        prop="city"
        label="市区"
        width="120">
      </el-table-column>
      <el-table-column
        prop="address"
        label="地址"
        width="300">
      </el-table-column>
      <el-table-column
        prop="zip"
        label="邮编"
        width="120">
      </el-table-column>
    </el-table>
    <el-dialog
      title="新增客户信息"
      :visible.sync="centerDialogVisible"
      width="50%"
      center>
      <div class="dialogCont">
        <el-form
          :model="ruleForm"
          :rules="rules"
          ref="ruleForm"
          label-width="100px"
          class="demo-ruleForm"
          :close-on-click-modal="false"
          :hide-required-asterisk="false">
          <el-form-item label="所属区域" prop="region">
            <el-select v-model="ruleForm.region" placeholder="请选择">
              <el-option label="区域一" value="区域一"></el-option>
              <el-option label="区域二" value="区域二"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="客户名称" prop="name">
            <el-input v-model="ruleForm.name"></el-input>
          </el-form-item>
          <el-form-item label="客户简称" prop="abbreviation">
            <el-input v-model="ruleForm.abbreviation"></el-input>
          </el-form-item>
          <el-form-item label="客户联系人" prop="contacts">
            <el-input v-model="ruleForm.contacts"></el-input>
          </el-form-item>
          <el-form-item label="联系电话" prop="phone">
            <el-input v-model="ruleForm.phone"></el-input>
          </el-form-item>
          <el-form-item label="客户地址" prop="address">
            <el-input v-model="ruleForm.address"></el-input>
          </el-form-item>
          <el-form-item label="是否代理商" prop="agent">
            <el-select v-model="ruleForm.agent" placeholder="请选择">
              <el-option label="是" value="是"></el-option>
              <el-option label="否" value="否"></el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </div>
        <div slot="footer" class="dialog-footer">
            <el-button @click="addCancel('ruleForm')">取 消</el-button>
            <el-button type="primary" @click="centerDialogVisible = false">确 定</el-button>
        </div>
    </el-dialog>
  </div>
</template>
<script>
export default {
  name: 'Protocol',
  data () {
    const validatePhone = (rule, value, callback) => {
      let reg = /^1[345789]\d{9}$/
      if (value !== '' && reg.test(value)) {
        callback()
      } else {
        callback(new Error('请输入正确的手机号'))
      }
    }
    return {
      centerDialogVisible: false,
      formInline: {
        customerName: '',
        customerAbbreviation: '',
        customerContact: '',
        customerPhone: '',
        customerAddress: ''
      },
      formInlineRules: {
        customerName: [
          { required: true, message: '请输入客户名称', trigger: 'blur' },
          { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        customerAbbreviation: [
          { required: true, message: '请输入客户简称', trigger: 'blur' },
          { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        customerContact: [
          { required: true, message: '请输入客户联系人', trigger: 'blur' }
        ],
        customerPhone: [
          { required: true, validator: validatePhone, message: '请填写正确的电话', trigger: 'change' }
        ],
        customerAddress: [
          { required: true, message: '请填写联系地址', trigger: 'blur' }
        ]
      },
      tableData3: [
        {
          date: '2016-05-03',
          name: '王小虎',
          province: '上海',
          city: '普陀区',
          address: '上海市普陀区金沙江路 1518 弄',
          zip: 200333
        },
        {
          date: '2016-05-03',
          name: '王小虎',
          province: '上海',
          city: '普陀区',
          address: '上海市普陀区金沙江路 1518 弄',
          zip: 200333
        },
        {
          date: '2016-05-03',
          name: '王小虎',
          province: '上海',
          city: '普陀区',
          address: '上海市普陀区金沙江路 1518 弄',
          zip: 200333
        },
        {
          date: '2016-05-03',
          name: '王小虎',
          province: '上海',
          city: '普陀区',
          address: '上海市普陀区金沙江路 1518 弄',
          zip: 200333
        },
        {
          date: '2016-05-03',
          name: '王小虎',
          province: '上海',
          city: '普陀区',
          address: '上海市普陀区金沙江路 1518 弄',
          zip: 200333
        },
        {
          date: '2016-05-03',
          name: '王小虎',
          province: '上海',
          city: '普陀区',
          address: '上海市普陀区金沙江路 1518 弄',
          zip: 200333
        },
        {
          date: '2016-05-03',
          name: '王小虎',
          province: '上海',
          city: '普陀区',
          address: '上海市普陀区金沙江路 1518 弄',
          zip: 200333
        },
        {
          date: '2016-05-03',
          name: '王小虎',
          province: '上海',
          city: '普陀区',
          address: '上海市普陀区金沙江路 1518 弄',
          zip: 200333
        },
        {
          date: '2016-05-03',
          name: '王小虎',
          province: '上海',
          city: '普陀区',
          address: '上海市普陀区金沙江路 1518 弄',
          zip: 200333
        },
        {
          date: '2016-05-03',
          name: '王小虎',
          province: '上海',
          city: '普陀区',
          address: '上海市普陀区金沙江路 1518 弄',
          zip: 200333
        },
        {
          date: '2016-05-03',
          name: '王小虎',
          province: '上海',
          city: '普陀区',
          address: '上海市普陀区金沙江路 1518 弄',
          zip: 200333
        },
        {
          date: '2016-05-03',
          name: '王小虎',
          province: '上海',
          city: '普陀区',
          address: '上海市普陀区金沙江路 1518 弄',
          zip: 200333
        },
        {
          date: '2016-05-03',
          name: '王小虎',
          province: '上海',
          city: '普陀区',
          address: '上海市普陀区金沙江路 1518 弄',
          zip: 200333
        }
      ],
      ruleForm: {
        name: '',
        region: '',
        abbreviation: '',
        contacts: '',
        phone: '',
        address: '',
        agent: ''
      },
      rules: {
        name: [
          { required: true, message: '请输入客户名称', trigger: 'blur' },
          { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        region: [
          { required: true, message: '请选择所属区域', trigger: 'change' }
        ],
        abbreviation: [
          { required: true, message: '请输入客户名称', trigger: 'blur' }
        ],
        contacts: [
          { required: true, message: '请输入客户简称', trigger: 'blur' }
        ],
        phone: [
          { required: true, validator: validatePhone, message: '请填写正确的电话', trigger: 'change' }
        ],
        address: [
          { required: true, message: '请填写联系地址', trigger: 'blur' }
        ],
        agent: [
          { required: true, message: '请选择是否是代理商', trigger: 'change' }
        ]
      }
    }
  },
  methods: {
    onSubmit (key) {
      if (key === 'query') {
        console.log('submit!')
      } else if (key === 'add') {
        this.centerDialogVisible = true
      }
    },
    addCancel (formName) {
      this.centerDialogVisible = false
      this.$refs[formName].resetFields()
    }
  }
}
</script>
<style scoped lang="less">
.parent{
  width: 100%;
  padding: 10px;
  background: #fff;
  border-radius: 5px;
}
</style>
